package cn.ccccltd.waf.id_generator.config.db;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.pool.DruidDataSource;


@Configuration
@Order(-100)
@ConditionalOnProperty(name = "spring.dbtype", havingValue = "db")
public class DBConfig {

	
	private static final Logger log = LoggerFactory.getLogger(DBConfig.class);

	
	@Autowired
	DBProperties dbProperties;
	
	@Bean(initMethod = "init", destroyMethod = "close")
	public DruidDataSource dataSource() {
		
		DruidDataSource dataSource = new DruidDataSource();
	    
		dataSource.setDriverClassName(dbProperties.getDriverClassName());
		dataSource.setUrl(dbProperties.getUrl());
		dataSource.setUsername(dbProperties.getUsername());
		dataSource.setPassword(dbProperties.getPassword());
		
		//初始化时建立物理连接的个数，缺省值为0
		dataSource.setInitialSize(dbProperties.getInitialSize());
		//最小连接池数量
		dataSource.setMinIdle(dbProperties.getMinIdle());
		//最大连接池数量，缺省值为8
		dataSource.setMaxActive(dbProperties.getMaxActive());
		/**
		 * 获取连接时最大等待时间，单位毫秒。配置了maxWait之后，缺省启用公平锁，
		 * 并发效率会有所下降，如果需要可以通过配置useUnfairLock属性为true使用非公平锁。
		 */
		dataSource.setMaxWait(dbProperties.getMaxWait());
		
        dataSource.setTimeBetweenEvictionRunsMillis(dbProperties.getTimeBetweenEvictionRunsMillis());    
        dataSource.setMinEvictableIdleTimeMillis(dbProperties.getMinEvictableIdleTimeMillis());    
        dataSource.setValidationQuery(dbProperties.getValidationQuery());    
        dataSource.setTestWhileIdle(dbProperties.getTestWhileIdle());    
        dataSource.setTestOnBorrow(dbProperties.getTestOnBorrow());    
        dataSource.setTestOnReturn(dbProperties.getTestOnReturn());    
        dataSource.setPoolPreparedStatements(dbProperties.getPoolPreparedStatements());    
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(dbProperties.getMaxPoolPreparedStatementPerConnectionSize());    
        
        try {    
        	
            dataSource.setFilters(dbProperties.getFilters());
            
        } catch (SQLException e) {

        	e.printStackTrace();
        	
        	log.error("druid configuration initialization filter fail", e); 
        	
        }    
        
        dataSource.setConnectionProperties(dbProperties.getConnectionProperties());  
		
		List<Filter> filtersBean = new ArrayList<>();
		
		dataSource.setProxyFilters(filtersBean);
		
		return dataSource;
	}
	
	@Bean
	public JdbcTemplate jdbcTemplate() {
		
		JdbcTemplate jt = new JdbcTemplate();
		jt.setDataSource(dataSource());
		
		return jt;
		
	}
	
	
	
}
